package com.guigu.buy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.buy.pojo.Cart;
import com.guigu.buy.pojo.CartOrderDto;
import com.guigu.buy.service.ICartService;
import com.guigu.buy.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author com.guigu
 * @since 2024-05-15
 */
@CrossOrigin
@Controller
@RequestMapping("/buy/cart")
public class CartController {
    @Autowired
    ICartService cartService;

    @ResponseBody
    @PostMapping("/addCart")
    public boolean addCart(@RequestBody CartVo vo){

        System.out.println(vo);
        Cart cart1=new Cart();
        cart1.setNum(vo.getNum());
        cart1.setCustomerId(vo.getConsumerId());
        cart1.setProductId(vo.getProductId());
        cart1.setCreateTime(new Date());
//        int i = cartService.addCart(cart);
        boolean i=cartService.save(cart1);
        return i;
    }

    /**
     * 根据商品ID查询购物车商品信息
     * @param customerId
     * @return
     */
    @ResponseBody
    @GetMapping("/showCart/{customerId}")
    public List<Cart> showCart(@PathVariable Integer customerId){
        System.out.println(customerId);
        List<Cart> carts = cartService.showCart(customerId);
        System.out.println(carts);
        return carts;
    }

    /**
     * 根据ID进行删除
     * @param id
     * @return
     */
    @DeleteMapping("/remove/{id}")
    @ResponseBody
    public String removeCartItem(@PathVariable Integer id) {
        QueryWrapper<Cart> qw = new QueryWrapper<Cart>();
        qw.eq("id",id);
        boolean remove = cartService.remove(qw);
        if(remove){
            return "ok";
        }
        return "error";
    }
    /**
     * 根据用户ID加载购物车
     * @param customerId
     * @return
     */
    @ResponseBody
    @GetMapping("/load")
    public List<Cart> loadCart(Integer customerId) {
        List<Cart> cartItems = cartService.loadCartByConsumerId(customerId);
        return cartItems;
    }
    @RequestMapping("/addOrder")
    @ResponseBody
    public String addOrder(CartOrderDto cartOrderDto){
        System.out.println(cartOrderDto);
        return "";
    }
}
